package com.estacionamientos

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices

class ConfirmController {

    def TokenBasedRememberMeServices rememberMeServices
	def DaoAuthenticationProvider daoAuthenticationProvider
	def AbstractUsuarioService abstractUsuarioService
	def AuthenticationManager authenticationManager
	
	def index = { 
		def token = params.token
		String [] parametros = rememberMeServices.decodeCookie(token)
		def usr = parametros[0]
		def pass = parametros[1]
		AbstractUsuario usuario = abstractUsuarioService.getUsuarioBy(usr, pass);
		usuario.setEnabled(true);
		abstractUsuarioService.saveMethod usuario	
		
		loggear(usuario)			
	}
	
	private void loggear(AbstractUsuario usuario){
		def usr = usuario.getUsername()
		def pass = daoAuthenticationProvider.getPasswordEncoder().decodePassword(usuario.getPassword());
		Authentication authentication = new UsernamePasswordAuthenticationToken(usr,pass);
		authenticationManager.authenticate authentication;
	}
}
